package com.lsht.web.controller.siwei;
import com.github.pagehelper.PageInfo;
import com.lsht.common.bo.IdListAcceptBo;
import com.lsht.common.core.controller.BaseController;
import com.lsht.common.core.domain.AjaxResult;
import com.lsht.common.core.domain.model.LoginUser;
import com.lsht.common.utils.ServletUtils;
import com.lsht.common.utils.uuid.IdUtils;
import com.lsht.framework.web.service.TokenService;
import com.lsht.system.domain.siwei.SiweiDevice;
import com.lsht.system.domain.siwei.SiweiGroup;
import com.lsht.system.domain.siwei.SiweiPoint;
import com.lsht.system.domain.vo.GroupPointVo;
import com.lsht.system.domain.vo.Region;
import com.lsht.system.service.siwei.SiweiGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
/**
 * 分组管理Controller
 *
 * @author zyc
 * @date 2024-08-14
 */
@RestController
@RequestMapping("/siwei/group")
@Api(value = "SiweiGroup", description = "分组管理", tags = "分组管理")
public class SiweiGroupController extends BaseController {
    @Autowired
    private SiweiGroupService siweiGroupService;
    @Autowired
    private TokenService tokenService;
    /**
     * 新增分组管理
     */
    @PostMapping(ADD_FUNCTION_URL)
    @ApiOperation(value = ADD_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "分组管理")
    //@PreAuthorize("@ss.hasPermi('siwei:group:add')")
    public AjaxResult genAdd(@RequestBody SiweiGroup siweiGroup) {
        siweiGroup.setId(IdUtils.simpleUUID());
        return toAjax(siweiGroupService.genAdd(siweiGroup));
    }

    /**
     * 删除分组管理
     */
    @PostMapping(DELETE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = DELETE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "分组管理")
   // @PreAuthorize("@ss.hasPermi('siwei:group:remove')")
    public AjaxResult genDeleteById(@RequestBody IdListAcceptBo idListAcceptBo) {
        List<String> idList = idListAcceptBo.getIdList();
        if (CollectionUtils.isNotEmpty(idList)) {
            return toAjax(siweiGroupService.genDeleteById(idList));
        }
        return AjaxResult.error();
    }

    /**
     * 修改分组管理
     */
    @PostMapping(UPDATE_BY_ID_FUNCTION_URL)
    @ApiOperation(value = UPDATE_BY_ID_FUNCTION, httpMethod = "POST", response = AjaxResult.class, tags = "分组管理")
    //@PreAuthorize("@ss.hasPermi('siwei:group:edit')")
    public AjaxResult genUpdateById(@RequestBody SiweiGroup siweiGroup) {
        return toAjax(siweiGroupService.genUpdateById(siweiGroup));
    }

    /**
     * 查询分组管理列表
     */
    @PostMapping(QUERY_LIST_FUNCTION_URL)
    @ApiOperation(value = QUERY_LIST_FUNCTION, httpMethod = "POST", response = SiweiGroup.class, tags = "分组管理")
    //@PreAuthorize("@ss.hasPermi('siwei:group:list')")
    public AjaxResult genQueryList(@RequestBody SiweiGroup siweiGroup) {
        startPage();
        List<SiweiGroup> list = siweiGroupService.genQueryList(siweiGroup);
        return AjaxResult.success(new PageInfo<>(list));
    }

    /**
     * 查询分组管理不分页列表
     */
    @PostMapping("notPageGenQueryList")
    @ApiOperation(value = "分组管理不分页列表", httpMethod = "POST", response = SiweiGroup.class, tags = "分组管理")
    public AjaxResult notPageGenQueryList(@RequestBody SiweiGroup siweiGroup) {
        List<SiweiGroup> list = siweiGroupService.genQueryList(siweiGroup);
        return AjaxResult.success(list);
    }

    /**
     * 根据id查询分组详情信息
     */
    @PostMapping(QUERY_BY_ID_FUNCTION_URL)
    @ApiOperation(value = QUERY_BY_ID_FUNCTION, httpMethod = "POST", response = SiweiDevice.class, tags = "分组管理")
    //@PreAuthorize("@ss.hasPermi('siwei:group:query')")
    public AjaxResult genQueryById(@RequestBody SiweiGroup siweiGroup) {
        SiweiGroup group = siweiGroupService.genQueryById(siweiGroup);
        return AjaxResult.success(group);
    }


    /**
     * 查询分组及点位信息
     */
    @PostMapping("getGroupPoint")
    @ApiOperation(value = "查询分组及点位信息", httpMethod = "POST", response = SiweiDevice.class, tags = "分组管理")
    public AjaxResult getGroupPoint(@RequestBody SiweiGroup siweiGroup) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        //获取部门ID
        Long deptId = loginUser.getUser().getDeptId();
        List<Region> groupPointVo = siweiGroupService.getGroupPoint(siweiGroup,deptId,loginUser);
        return AjaxResult.success(groupPointVo);
    }

    /**
     * 大屏查询分组数量及昨日新增数
     */
    @PostMapping("groupStatistics")
    @ApiOperation(value = "大屏查询分组数量及昨日新增数", httpMethod = "POST", response = SiweiPoint.class, tags = "分组管理")
    public AjaxResult groupStatistics(@RequestBody SiweiGroup siweiGroup) {
        HashMap map = siweiGroupService.groupStatistics(siweiGroup);
        return AjaxResult.success(map);
    }


}
